/*
 * JsnapshotView.java
 */
package jsnapshot;

import java.awt.AWTException;
import java.awt.Cursor;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JWindow;
import javax.swing.filechooser.FileFilter;

/**
 * The application's main frame.
 */
public class JsnapshotView extends FrameView {

    public JsnapshotView(SingleFrameApplication app) {
        super(app);

        initComponents();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String) (evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer) (evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });

        try {
            image = new Robot().createScreenCapture(
                    new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
//            Dimension previewSize = jPreviewImageLabel.getSize();
            setPreviewImage(image);
//            selectionArea = new SelectionArea(new ImageIcon(image));
        } catch (AWTException ex) {
            Logger.getLogger(JsnapshotView.class.getName()).log(Level.SEVERE, null, ex);
        }
//        GraphicsDevice myDevice = null;
//        Window myWindow = null;
//        try {
//            boolean b = myDevice.isFullScreenSupported();
//            myDevice.setFullScreenWindow(myWindow);
//        } finally {
//
//        }
        ImageIcon icon = new ImageIcon(getClass().getResource("resources/Crystal_16_ksnapshot.png"));
        getFrame().setIconImage(icon.getImage());
    }

    public void setPreviewImage(BufferedImage img) {
        int xP = jPreviewImageLabel.getSize().width;
        int yP = jPreviewImageLabel.getSize().height;
        int xF = img.getWidth();
        int yF = img.getHeight();

        if (xP == 0 || yP == 0) {
            xP = 350;
            yP = 197;
        }
        int xFnew, yFnew;

        if (((double) xP / (double) yP) > ((double) xF / (double) yF)) {
            yFnew = yP;
            xFnew = (int) Math.ceil((double) xF / (double) yF * yFnew);
        } else {
            xFnew = xP;
            yFnew = (int) Math.ceil((double) yF / (double) xF * xFnew);
        }

        jPreviewImageLabel.setText(null);
        jPreviewImageLabel.setIcon(
                new ImageIcon(img.getScaledInstance(
                xFnew, yFnew, Image.SCALE_FAST)));
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = JsnapshotApp.getApplication().getMainFrame();
            aboutBox = new JsnapshotAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        JsnapshotApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPreviewImageLabel = new javax.swing.JLabel();
        jNewSnapshotButton = new javax.swing.JButton();
        jSaveAsButton = new javax.swing.JButton();
        jCopyToClipboardButton = new javax.swing.JButton();
        jPrintButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jCaptureModeComboBox = new javax.swing.JComboBox();
        jDelaySpinner = new javax.swing.JSpinner();
        jQuitButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jWindowDecorationsCheckBox = new javax.swing.JCheckBox();
        jHelpButton = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mainPanelMouseClicked(evt);
            }
        });

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(jsnapshot.JsnapshotApp.class).getContext().getResourceMap(JsnapshotView.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jPreviewImageLabel.setFont(resourceMap.getFont("jPreviewImageLabel.font")); // NOI18N
        jPreviewImageLabel.setForeground(resourceMap.getColor("jPreviewImageLabel.foreground")); // NOI18N
        jPreviewImageLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jPreviewImageLabel.setIcon(resourceMap.getIcon("jPreviewImageLabel.icon")); // NOI18N
        jPreviewImageLabel.setText(resourceMap.getString("jPreviewImageLabel.text")); // NOI18N
        jPreviewImageLabel.setName("jPreviewImageLabel"); // NOI18N
        jPreviewImageLabel.setPreferredSize(new java.awt.Dimension(350, 197));

        jNewSnapshotButton.setText(resourceMap.getString("jNewSnapshotButton.text")); // NOI18N
        jNewSnapshotButton.setActionCommand(resourceMap.getString("jNewSnapshotButton.actionCommand")); // NOI18N
        jNewSnapshotButton.setName("jNewSnapshotButton"); // NOI18N
        jNewSnapshotButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jNewSnapshotButtonActionPerformed(evt);
            }
        });

        jSaveAsButton.setText(resourceMap.getString("jSaveAsButton.text")); // NOI18N
        jSaveAsButton.setName("jSaveAsButton"); // NOI18N
        jSaveAsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSaveAsButtonActionPerformed(evt);
            }
        });

        jCopyToClipboardButton.setText(resourceMap.getString("jCopyToClipboardButton.text")); // NOI18N
        jCopyToClipboardButton.setName("jCopyToClipboardButton"); // NOI18N
        jCopyToClipboardButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCopyToClipboardButtonActionPerformed(evt);
            }
        });

        jPrintButton.setText(resourceMap.getString("jPrintButton.text")); // NOI18N
        jPrintButton.setEnabled(false);
        jPrintButton.setName("jPrintButton"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jCaptureModeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Full Screen", "Window Under Cursor", "Region", "Section of Window" }));
        jCaptureModeComboBox.setSelectedIndex(2);
        jCaptureModeComboBox.setName("jCaptureModeComboBox"); // NOI18N
        jCaptureModeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCaptureModeComboBoxActionPerformed(evt);
            }
        });

        jDelaySpinner.setEnabled(false);
        jDelaySpinner.setName("jDelaySpinner"); // NOI18N

        jQuitButton.setText(resourceMap.getString("jQuitButton.text")); // NOI18N
        jQuitButton.setName("jQuitButton"); // NOI18N
        jQuitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jQuitButtonActionPerformed(evt);
            }
        });

        jSeparator1.setName("jSeparator1"); // NOI18N

        jWindowDecorationsCheckBox.setSelected(true);
        jWindowDecorationsCheckBox.setText(resourceMap.getString("jWindowDecorationsCheckBox.text")); // NOI18N
        jWindowDecorationsCheckBox.setEnabled(false);
        jWindowDecorationsCheckBox.setName("jWindowDecorationsCheckBox"); // NOI18N

        jHelpButton.setText(resourceMap.getString("jHelpButton.text")); // NOI18N
        jHelpButton.setEnabled(false);
        jHelpButton.setName("jHelpButton"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(jWindowDecorationsCheckBox)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 473, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jDelaySpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()
                                .addComponent(jPreviewImageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jNewSnapshotButton)
                                    .addComponent(jSaveAsButton)
                                    .addComponent(jPrintButton)
                                    .addComponent(jCopyToClipboardButton)))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jCaptureModeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 391, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()
                                .addComponent(jHelpButton)
                                .addGap(367, 367, 367)
                                .addComponent(jQuitButton)))
                        .addGap(111, 111, 111))))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPreviewImageLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(jNewSnapshotButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jSaveAsButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCopyToClipboardButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPrintButton)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jCaptureModeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jDelaySpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jWindowDecorationsCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jQuitButton)
                    .addComponent(jHelpButton))
                .addGap(43, 43, 43))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(jsnapshot.JsnapshotApp.class).getContext().getActionMap(JsnapshotView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 594, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 424, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void jCaptureModeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCaptureModeComboBoxActionPerformed
        // TODO add your handling code here:
        switch (jCaptureModeComboBox.getSelectedIndex()) {
            case 0: // Full Screen
                jDelaySpinner.setEnabled(true);
                jWindowDecorationsCheckBox.setEnabled(false);
                break;
            case 1: // Window Under Cursor
                jDelaySpinner.setEnabled(true);
                jWindowDecorationsCheckBox.setEnabled(true);
                break;
            case 2: // Region
                jDelaySpinner.setEnabled(false);
                jWindowDecorationsCheckBox.setEnabled(false);
                break;
            case 3: // Section of Window
                jDelaySpinner.setEnabled(true);
                jWindowDecorationsCheckBox.setEnabled(false);
                break;
        }
    }//GEN-LAST:event_jCaptureModeComboBoxActionPerformed

    private void jQuitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jQuitButtonActionPerformed
        // TODO add your handling code here:
        JsnapshotApp.getApplication().exit();
    }//GEN-LAST:event_jQuitButtonActionPerformed

    private void mainPanelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mainPanelMouseClicked
        // TODO add your handling code here:
        System.out.println("x=" + evt.getLocationOnScreen().getX() + ", y=" + evt.getLocationOnScreen().getY());
    }//GEN-LAST:event_mainPanelMouseClicked

    private void jNewSnapshotButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jNewSnapshotButtonActionPerformed
        // TODO add your handling code here:
        this.getFrame().setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.getFrame().dispose();
        try {
            // waits for the window disappears after the fucking animation
            Thread.sleep(150);
        } catch (InterruptedException ex) {
            Logger.getLogger(JsnapshotView.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            image = new Robot().createScreenCapture(
                    new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
//            Dimension previewSize = jPreviewImageLabel.getSize();
//            setPreviewImage(image);
        } catch (AWTException ex) {
            Logger.getLogger(JsnapshotView.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.getFrame().setVisible(true);
        switch (jCaptureModeComboBox.getSelectedIndex()) {
            case 0: // Full Screen
                break;
            case 1: // Window Under Cursor
                break;
            case 2: // Region
                GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                GraphicsDevice gd = ge.getDefaultScreenDevice();
                win = new JWindow();
                selectionArea = new SelectionArea(new ImageIcon(image));
                win.getContentPane().add(selectionArea);
                win.setSize(image.getWidth(), image.getHeight());
                win.setVisible(true);
                win.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
                gd.setFullScreenWindow(win);
                break;
            case 3: // Section of Window
                break;
        }

//        this.getFrame().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

    }//GEN-LAST:event_jNewSnapshotButtonActionPerformed

    private void jCopyToClipboardButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCopyToClipboardButtonActionPerformed
        // TODO add your handling code here:

        Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();

        switch (jCaptureModeComboBox.getSelectedIndex()) {
            case 0: // Full Screen
                break;
            case 1: // Window Under Cursor
                break;
            case 2: // Region
                cb.setContents(new Transferable() {

                    public DataFlavor[] getTransferDataFlavors() {
                        return new DataFlavor[]{DataFlavor.imageFlavor};
                    }

                    public boolean isDataFlavorSupported(DataFlavor flavor) {
                        return DataFlavor.imageFlavor.equals(DataFlavor.imageFlavor);
                    }

                    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
                        if (!DataFlavor.imageFlavor.equals(flavor)) {
                            throw new UnsupportedFlavorException(flavor);
                        }
                        return image;
                    }
                }, null);
                break;
            case 3: // Section of Window
                break;
        }
    }//GEN-LAST:event_jCopyToClipboardButtonActionPerformed

    private void jSaveAsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSaveAsButtonActionPerformed
        // TODO add your handling code here:
        String pngFilePath = "";
        try {
            JFileChooser chooser = new JFileChooser(JsnapshotApp.getApplication().getLastDir());
            chooser.setDialogTitle("Save to image file ...");
            chooser.setFileFilter(new FileFilter() {

                public String getDescription() {
                    return "GIF file (*.gif)";
                }

                public boolean accept(File f) {
                    if (f.isDirectory()) {
                        return true;
                    }
                    if (f.getName().toLowerCase().endsWith(".gif")) {
                        return true;
                    }
                    return false;
                }
            });
            chooser.addChoosableFileFilter(new FileFilter() {

                public String getDescription() {
                    return "JPG files (*.jpg)";
                }

                public boolean accept(File f) {
                    if (f.isDirectory()) {
                        return true;
                    }
                    if (f.getName().toLowerCase().endsWith(".jpg")) {
                        return true;
                    }
                    return false;
                }
            });
            chooser.addChoosableFileFilter(new FileFilter() {

                public String getDescription() {
                    return "BMP files (*.bmp)";
                }

                public boolean accept(File f) {
                    if (f.isDirectory()) {
                        return true;
                    }
                    if (f.getName().toLowerCase().endsWith(".bmp")) {
                        return true;
                    }
                    return false;
                }
            });
            chooser.addChoosableFileFilter(new FileFilter() {

                public String getDescription() {
                    return "PNG files (*.png)";
                }

                public boolean accept(File f) {
                    if (f.isDirectory()) {
                        return true;
                    }
                    if (f.getName().toLowerCase().endsWith(".png")) {
                        return true;
                    }
                    return false;
                }
            });
            chooser.setAcceptAllFileFilterUsed(false);
            int returnVal = chooser.showSaveDialog(null);
//            chooser.getFileFilter().getDescription()
//            String[] list = ImageIO.getWriterFileSuffixes();
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                String ft = "png";
                pngFilePath = chooser.getSelectedFile().getAbsolutePath();
                if (chooser.getFileFilter().getDescription().contains("png")) {
                    ft = "png";
                    if (!pngFilePath.endsWith(".png")) {
                        pngFilePath += ".png";
                    }
                }
                if (chooser.getFileFilter().getDescription().contains("gif")) {
                    ft = "gif";
                    if (!pngFilePath.endsWith(".gif")) {
                        pngFilePath += ".gif";
                    }
                }
                if (chooser.getFileFilter().getDescription().contains("bmp")) {
                    ft = "bmp";
                    if (!pngFilePath.endsWith(".bmp")) {
                        pngFilePath += ".bmp";
                    }
                }
                if (chooser.getFileFilter().getDescription().contains("jpg")) {
                    ft = "jpg";
                    if (!pngFilePath.endsWith(".jpg")) {
                        pngFilePath += ".jpg";
                    }
                }
                JsnapshotApp.getApplication().setLastDir(chooser.getSelectedFile().getPath());
                File file = new File(pngFilePath);
                ImageIO.write(image, ft, file);
            }
        } catch (IOException eccc) {
        }
    }//GEN-LAST:event_jSaveAsButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jCaptureModeComboBox;
    private javax.swing.JButton jCopyToClipboardButton;
    private javax.swing.JSpinner jDelaySpinner;
    private javax.swing.JButton jHelpButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton jNewSnapshotButton;
    private javax.swing.JLabel jPreviewImageLabel;
    private javax.swing.JButton jPrintButton;
    private javax.swing.JButton jQuitButton;
    private javax.swing.JButton jSaveAsButton;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JCheckBox jWindowDecorationsCheckBox;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private JDialog aboutBox;
    private BufferedImage image;
//    private captureDialog captureScreen = new captureDialog(JsnapshotApp.getApplication().getMainFrame(), true);
    private JWindow win;
//    private Clipboard clipboard = new Clipboard(name);
    private SelectionArea selectionArea;

    /**
     * @return the win
     */
    public JWindow getWin() {
        return win;
    }

    /**
     * @param win the win to set
     */
    public void setWin(JWindow win) {
        this.win = win;
    }

    /**
     * @return the image
     */
    public BufferedImage getImage() {
        return image;
    }

    /**
     * @param image the image to set
     */
    public void setImage(BufferedImage image) {
        this.image = image;
    }
}
